package com.olymtech.jbricks.threadpool.alarm;

import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.util.ConfigUtil;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ForkJoinPool;

/**
 * @author haoting.wang
 * @date 2020-06-15 14:52
 **/
@Slf4j
public class WeChatRobotAlarm {


    private static ObjectMapper mapper = new ObjectMapper();

    {
        mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
    }

    public static void sendAlarm2Robot(String text, String url) {
        ConfigUtil configUtil = new ConfigUtil();
        Env env = configUtil.getApolloEnv();

        text = "环境:" + env.name() + " 报错原因:" + text;
        MachineContent content = new MachineContent();
        content.setMsgtype("text");
        MachineContentText contentText = new MachineContentText();
        contentText.setContent(text);
        contentText.setMentioned_list(Arrays.asList(new String[]{"@all"}));
        content.setText(contentText);

        String contentStr = "";
        try {
            contentStr = contentStr + mapper.writeValueAsString(content);
            final String finalContentStr = contentStr;
            post(url,finalContentStr);

        } catch (Exception e) {
            log.error("告警消息发送失败,数据内容:{}", text);
        }
    }


    public static void post(String url, String jsonBody) {
        HttpURLConnection conn = null;
        try {
            URL httpUrl = new URL(url);
            conn = (HttpURLConnection) httpUrl.openConnection();
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Charset", "UTF-8");
            conn.setRequestProperty("Content-Type", "application/Json; charset=UTF-8");
            conn.connect();
            OutputStream out = conn.getOutputStream();
            byte[] data = jsonBody.getBytes();
            out.write(data);
            out.flush();
            out.close();
            InputStream in = conn.getInputStream();
            byte[] data1 = new byte[in.available()];
            in.read(data1);
            System.out.println(new String(data1));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (conn != null) {
                conn.disconnect();
            }
        }
    }

    static class MachineContent {
        /**
         * 支持text&markdown
         */
        private String msgtype;
        private MachineContentText text;

        public String getMsgtype() {
            return msgtype;
        }

        public void setMsgtype(String msgtype) {
            this.msgtype = msgtype;
        }

        public MachineContentText getText() {
            return text;
        }

        public void setText(MachineContentText text) {
            this.text = text;
        }
    }

    static class MachineContentText {
        private String content;
        private List<String> mentioned_list;

        public String getContent() {
            return content;
        }

        public void setContent(String content) {
            this.content = content;
        }

        public List<String> getMentioned_list() {
            return mentioned_list;
        }

        public void setMentioned_list(List<String> mentioned_list) {
            this.mentioned_list = mentioned_list;
        }
    }
}
